package com.lushuai.jiubeidanci;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.lushuai.jiubeidanci.dao.UserDao;
import com.lushuai.jiubeidanci.model.User;

public class LoginActivity extends AppCompatActivity {
    private EditText etNickname;
    private EditText etPassword;
    private Button btnLogin;
    private TextView tvGuestLogin;
    private TextView tvRegister;
    private UserDao userDao;
    private SharedPreferences sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        // 检查登录状态
        sp = getSharedPreferences("user_info", MODE_PRIVATE);
        if (checkLoginStatus()) {
            startMainActivity();
            finish();
            return;
        }
        
        setContentView(R.layout.activity_login);
        
        // 初始化视图和DAO
        initViews();
        userDao = new UserDao(this);
        
        // 设置点击事件
        setClickListeners();
    }
    
    private void initViews() {
        etNickname = findViewById(R.id.et_nickname);
        etPassword = findViewById(R.id.et_password);
        btnLogin = findViewById(R.id.btn_login);
        tvGuestLogin = findViewById(R.id.guest_login);
        tvRegister = findViewById(R.id.register_text);
    }
    
    private void setClickListeners() {
        // 登录按钮点击事件
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                login();
            }
        });
        
        // 游客登录点击事件
        tvGuestLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                guestLogin();
            }
        });
        
        // 注册点击事件
        tvRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startRegisterActivity();
            }
        });
    }
    
    private boolean checkLoginStatus() {
        long userId = sp.getLong("user_id", -1);
        return userId != -1;
    }
    
    private void login() {
        String nickname = etNickname.getText().toString().trim();
        String password = etPassword.getText().toString().trim();
        
        // 输入验证
        if (TextUtils.isEmpty(nickname) || TextUtils.isEmpty(password)) {
            Toast.makeText(this, "用户名和密码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        
        // 验证用户名和密码
        User user = userDao.login(nickname, password);
        if (user != null) {
            // 保存登录状态
            saveLoginStatus(user);
            // 更新最后登录时间
            user.setLastLoginTime(System.currentTimeMillis());
            userDao.update(user);
            // 跳转到主界面
            startMainActivity();
            finish();
        } else {
            Toast.makeText(this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
        }
    }
    
    private void guestLogin() {
        // 游客模式直接进入主界面，不保存登录状态
        startMainActivity();
        finish();
    }
    
    private void saveLoginStatus(User user) {
        SharedPreferences.Editor editor = sp.edit();
        editor.putLong("user_id", user.getUserId());
        editor.putString("nickname", user.getNickname());
        editor.putBoolean("is_vip", user.isVip());
        editor.apply();
    }
    
    private void startMainActivity() {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
    }
    
    private void startRegisterActivity() {
        Intent intent = new Intent(this, RegisterActivity.class);
        startActivity(intent);
    }
} 